var FBook = {};

var isFaceBook = true;

/**
 *   fb  借口统一管理
 * @param type 请求fb 借口类型
 * @param str  接受的字符串类型
 * @param backfun  成功后的回调
 * @returns {*}
 */
FBook.getInfoType = function (type, str, backfun) {
    if (isFaceBook) {
        switch (type) {
            case "getName":                                             /*获取个人名字*/
                return getName();
                break;
            case "getID":                                               /*获取获取个人Id*/
                return getID();
                break;
            case "getPhoto":                                            /*获取个人头像*/
                return getPhoto(backfun);
                break;
            case "getLocale":                                           /*获取个人地址*/
                return getLocale();
                break;
            case "getUserInfo":                                         /*获取个人信息*/
                return getUserInfo();
                break;
            case "setRankInfoForFaceBook":                              /*提交排行信息*/
                return setRankInfoForFaceBook();
                break;
            case "getMySelfRankInfoForFaceBook":                        /*获取好友排行信息*/
                return getMySelfRankInfoForFaceBook(backfun);
                break;
            case "getAllRankInfoForFaceBook":                           /*获取所有排行信息*/
                return getAllRankInfoForFaceBook();
                break;
            case "logEvent":                                            /*统计*/
                logEvent(str);
                break;
            case "canCreateShortcutAsync":                              /*判断能否创建快捷方式*/
                canCreateShortcutAsync(backfun);
                break;
            case "createShortcutAsync":                                 /*创建快捷方式*/
                createShortcutAsync(backfun);
                break;
            case "preloadAdWeed":                                       /*预加载广告*/
                preloadAdWeed(str, backfun);
                break;
            case "playAdWeed":                                          /*播放广告*/
                playAdWeed(str, backfun);
                break;
        }
    } else {
        return null;
    }

};

function logEvent(str) {
    FBInstant.logEvent(str);
};
function preloadAdWeed(_id, backfun) {
    var obj = {
        id: _id,
        adWeed: null,
        state: false
    };
    if (cc.sys.isMobile) {
        FBInstant.getRewardedVideoAsync(
            _id,
        ).then(function (rewardedVideo) {
            obj.adWeed = rewardedVideo;
            obj.adWeed.loadAsync();
        }).then(function () {
            obj.state = true;
            backfun(obj);
            return obj;
        }).catch(function (err) {
            setTimeout(function () {
                // preloadAdWeed(_id, backfun);
            }, 30000);
            console.error('Interstitial failed to preload: ' + err.message);
        });
    }
};
function playAdWeed(obj, backfun) {
    if (cc.sys.isMobile) {
        obj.adWeed.showAsync()
            .then(function () {
                backfun();
            })
            .catch(function (e) {

            });
    }
}

function createShortcutAsync(backfun) {
    FBInstant.createShortcutAsync()
        .then(function () {
            console.info("发送快捷方式到桌面  成功");
            if (backfun) {
                backfun(1);
            }
        }).catch(function () {
        backfun(0);
        console.info("发送快捷方式到桌面  异常");
    });
};

function canCreateShortcutAsync(backfun) {
    // console.info("发送快捷方式到桌面");
    FBInstant.canCreateShortcutAsync()
        .then(function (canCreateShortcut) {
            if (canCreateShortcut) {
                backfun();
                console.info("可以发送快捷方式到桌面");
            } else {

                // console.info("不可以发送快捷方式到桌面");
            }
        });
};

function getName() {
    const playerName = FBInstant.player.getName();
    return playerName;
};

function getID(count) {
    const userID = FBInstant.player.getID();
    return userID;

};

function getPhoto(backfun) {
    // backfun("sssss");
    const playerPic = FBInstant.player.getPhoto();
    backfun(playerPic);
    // return playerPic;
};

function getLocale() {
    const playerLocal = FBInstant.getLocale();
    return playerLocal;
};

//设置排行榜的数据  //可以往里面传参数
function setRankInfoForFaceBook(score) {
    console.log("FaceBookUtils--->setRankInfoForFaceBook--->uploading Score ！！！", score)
    if (typeof FBInstant === 'undefined') return;
    score = (score).toFixed(0);
    score = Number(score);
    // console.log("给facebook上传自己的分数  排行榜使用",score)
    FBInstant.getLeaderboardAsync(worldRank)    //通过名字 回去排行榜
        .then(function (leaderboard) {
            // console.log("leaderboard.name",leaderboard.getName());
            return leaderboard.setScoreAsync(score);
        })
        .then(() => LogUtils.log('Score saved'))
        .catch(function (error) {
            // console.log("error",error)
        });
};

//获取玩家自己的排行榜信息
function getMySelfRankInfoForFaceBook(backFun) {
    var worldRank = "rank";
    if (typeof FBInstant === 'undefined') return;
    FBInstant.getLeaderboardAsync(worldRank)
        .then(leaderboard => leaderboard.getPlayerEntryAsync())
        .then(entry => {
            console.log("FaceBookUtils--->getMySelfRankInfoForFaceBook--->entry", entry);
            if (entry && entry.getRank) {
                var myIndex = entry.getRank();
                console.log("FaceBookUtils--->getMySelfRankInfoForFaceBook--->myIndex", myIndex);
                if (backFun) {
                    backFun(myIndex)
                }
            } else {
                console.log("entry.getRank error！！！")
            }

        }).catch(error => console.error(error));
};

//获取全部的排行榜信息
function getAllRankInfoForFaceBook(backFun) {
    if (typeof FBInstant === 'undefined') return;
    FBInstant.getLeaderboardAsync(worldRank)
        .then(leaderboard => leaderboard.getEntriesAsync(10, 0))
        .then(entries => {
            console.log("entries", entries);
            if (backFun) {
                backFun(entries)
            }
        }).catch(error => console.error(error));
};

function getUserInfo() {
    var userInfo = {};
    userInfo.playerName = FBInstant.player.getName();
    userInfo.userID = FBInstant.player.getID();
    userInfo.playerPic = FBInstant.player.getPhoto();
    userInfo.playerLocal = FBInstant.getLocale();
    return userInfo;
};

module.exports = FBook;